package org.sixtynine.stock.dao.impl;

/**
 * @author Thilan
 *
 */

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.sixtynine.stock.Constants;
import org.sixtynine.stock.dao.DailyShareDataDao;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.DailyShareData;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class DailyShareDataDaoImpl extends
		AbstractDaoImpl<DailyShareData, Integer> implements DailyShareDataDao {

	protected DailyShareDataDaoImpl() {
		super(DailyShareData.class);
	}

	@Override
	public void saveDailyShareData(DailyShareData dailyShareData) {
		saveOrUpdate(dailyShareData);
	}

	@Override
	public List<DailyShareData> findDailyShareDatas(int companyId) {
		return findByCriteria(Restrictions.eq("companyId", companyId));
	}

	@Override
	@Transactional
	public void deleteDailyShareData(int companyId, Date fromDate, Date toDate) {
		String sql = "delete from DailyShareData where 1=1 ";
		if (companyId > Constants.ERROR) {
			sql += "and company_id = :companyId ";
		}
		if (fromDate != null) {
			sql += "and date >= :fromData ";
		}
		if (toDate != null) {
			sql += "and date <= :toDate ";
		}

		Query query = getCurrentSession().createQuery(sql);
		if (companyId > Constants.ERROR) {
			query.setInteger("companyId", companyId);
		}
		if (fromDate != null) {
			query.setParameter("fromData", fromDate);
		}
		if (toDate != null) {
			query.setParameter("toDate", toDate);
		}

		query.executeUpdate();

	}

	@Override
	public List<DailyShareData> searchDailyShareData(int companyId,
			Date fromDate, Date toDate, String orderBy, String orderByName) {
		Criteria criteria = getCurrentSession().createCriteria(
				DailyShareData.class);

		criteria.addOrder(Order.desc(getRecordOrder(orderBy)));
		criteria.addOrder(Order.asc(getRecordOrder(orderByName)));

		if (companyId > Constants.ERROR) {
			criteria.add(Restrictions.eq("company.id", companyId));
		}
		if (fromDate != null) {
			criteria.add(Restrictions.ge("date", fromDate));
		}

		if (toDate != null) {
			criteria.add(Restrictions.le("date", toDate));
		}

		return findByCriteria(criteria);
	}

	@Override
	public Page<DailyShareData> searchDailyShareData(int companyId,
			Date fromDate, Date toDate, String orderBy, String orderByName,
			GridPramDTO gridPramDTO) {
		Criteria criteria = getCurrentSession().createCriteria(
				DailyShareData.class);

		if (orderBy != null) {
			criteria.addOrder(Order.desc(getRecordOrder(orderBy)));
			if (orderByName != null) {
				criteria.addOrder(Order.asc(getRecordOrder(orderByName)));
			}
		} else {
			criteria.addOrder(Order.desc("date"));
			
			criteria.createAlias("company", "cmp");
			criteria.addOrder(Order.asc("cmp.code"));
		}

		if (companyId > Constants.ERROR) {
			criteria.add(Restrictions.eq("company.id", companyId));
		}
		if (fromDate != null) {
			criteria.add(Restrictions.ge("date", fromDate));
		}

		if (toDate != null) {
			criteria.add(Restrictions.le("date", toDate));
		}

		return findByCriteria(criteria, gridPramDTO);
	}
}
